import { defHttp } from '@/utils/http';

interface TriggerPipelineParam {
  pipelineCode: string;
  remark?: string;
}

// 查询流水线任务定义
export const getTaskDef = (uuid: string) => defHttp.get({ url: `/devops/task-def/${uuid}` });

// 获取流水线配置信息 code: 流水线编码
export const getPipelineConfig = (code: string) => defHttp.get({ url: `/devops/pipeline/pipelineConfig/${code}` });

// 更新流水线
export const updatePipeline = (data: Pipeline.PipelineConfig) =>
  defHttp.request({ url: '/devops/pipeline', method: 'put', data });

// 删除流水线
export const deletePipeline = (code: string) => defHttp.request({ url: `/devops/${code}`, method: 'delete' });

// 查询流水线标签列表(有过滤条件)
export const getTagList = (data: Pipeline.TagList) => defHttp.post({ url: '/devops/tag/list', data });

// 新增流水线标签
export const addTagList = (data: Pipeline.TagList) => defHttp.post({ url: '/devops/tag', data });

// 查询流水线分组列表(有过滤条件)
export const getGroupList = (data: Pipeline.GroupList) => defHttp.post({ url: '/devops/group/list', data });

// 查询流水线任务定义列表
export const getTaskDefList = () => defHttp.post({ url: '/devops/task-def/assemblyList' });

// 查询流水线yaml配置
export const getYamlConfig = (code: string) => defHttp.get({ url: `/devops/pipeline/yamlConfig/${code}` });

// 根据流水线配置态编码获取流水线运行态实例
export const getPipelineInstanceInfo = (flag: number, pipelineCode: string) =>
  defHttp.get({ url: `/devops/pipelineInstance/info/${flag}/${pipelineCode}` });

// 查询运行历史
export const getPipelineDevHistroy = (data: string) => defHttp.post({ url: `/devops/pipelineInstance/page`, data });

// 删除标签
export const deleteUuidTag = (uuid: number | string) => defHttp.delete({ url: `/devops/tag/${uuid}` });

// 查询我的流水线列表
export const getMyPipelineList = (data: string) => defHttp.post({ url: `/devops/pipeline/pageMyPipeline`, data });

// 流水线分组变更
export const getModifyPipelineGroupsList = (data: any) =>
  defHttp.post({ url: `/devops/group/modifyPipelineGroups`, data });

// 流水线标签变更
export const getModifyPipelineTagsList = (data: any) => defHttp.post({ url: `/devops/tag/modifyPipelineTags`, data });

// 批量新增/更新流水线分组
export const updataBatchSaveOrUpdateList = (data: any) =>
  defHttp.post({ url: `/devops/group/batchSaveOrUpdate`, data });

// 批量新增/更新流水线标签
export const updataBatchTagSaveOrList = (data: any) => defHttp.post({ url: `/devops/tag/batchSaveOrUpdate`, data });

// 根据关键字（应用名）获取应用数组
export const getListAppByKeyword = (keyword: string) =>
  defHttp.get({ url: `/devops/application/listAppByKeyword?keyword=${keyword}` });

// 根据关键字（组件名称）来查询组件数组
export const getListComponentByKeyword = (keyword: string) =>
  defHttp.get({ url: `/devops/component/listComponentByKeyword?keyword=${keyword}` });

// 根据关键字（环境名称）查询环境数组
export const getListEnvironmentByKeyword = (keyword: string) =>
  defHttp.get({ url: `/devops/environment/listEnvironmentByKeyword?keyword=${keyword}` });

// 获取触发方式枚举下拉选项
export const getListTriggerWay = () => defHttp.get({ url: `/devops/pipeline/listTriggerWay` });

// 查询流水线
export const getPipelineData = (code: string | number) => defHttp.get({ url: `/devops/pipeline/${code}` });

// 获取流水线实例状态枚举下拉选项
export const getListInstanceStatus = () => defHttp.get({ url: `/devops/pipeline/listPipelineInstanceStatus` });

// 流水线触发接口
export const triggerPipeline = (data: TriggerPipelineParam) =>
  defHttp.post({ url: '/devops/pipelineInstance/trigger/pipeline', data });

// 获取任务日志
export const getTaskLog = (paramStr: string) =>
  defHttp.get({ url: `/devops/pipeline/pipeline/taskLog/download${paramStr}` }, { isReturnNativeResponse: true });

// 收藏流水线
export const collectionLine = (code: string) => defHttp.post({ url: `devops/group/addFavoritePipeline?code=${code}` });

// 取消收藏流水线
export const cancelCollectionLine = (code: string) =>
  defHttp.post({ url: `/devops/group/removeFavoritePipeline?code=${code}` });

// 流水线终止
export const stopPipeline = (pipelineCode: string) =>
  defHttp.post({ url: `/devops/pipelineInstance/cancel/pipeline/${pipelineCode}` });

// 批量触发流水线
export const batchTriggerPipeline = (data: string[]) =>
  defHttp.post({ url: '/devops/pipelineInstance/trigger/batch/pipeline', data });
